/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

/**
 *
 * @author TranDangThuan
 */
public class DBUtility {
    public static Connection openConnection() {
        Connection con = null;
        try {
            ResourceBundle bun = ResourceBundle.getBundle("control.config");
            Class.forName(bun.getString("driver"));
            String url = "jdbc:sqlserver://" + bun.getString("server") + ":";
            url += bun.getString("port") + ";databaseName=CrimeFile";
            con = DriverManager.getConnection(url, bun.getString("user"), bun.getString("pass"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return con;
    }
    
    public static void closeResultSet(ResultSet rs) {
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
    public static void closeStatement(PreparedStatement stm) {
        if(stm != null){
            try {
                stm.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
    public static void closeConnection(Connection con) {
        if(con != null){
            try {
                con.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }
}
